package com.haimi.shiguang.base;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.haimi.shiguang.utils.ViewToolsUtil;

import io.reactivex.disposables.CompositeDisposable;

/**
 * @author changsunhaipeng
 */
public abstract class BaseFragment extends Fragment implements Host, OnKeyBackListener, View.OnClickListener {
    public String TAG = this.getClass().getName();
    public CompositeDisposable compositeDisposable = new CompositeDisposable();

    public abstract void initView();

    public abstract void initListener();

    public abstract void initData();

    protected View rootView;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rootView = view;
        initView();
        initData();
        initListener();
    }

    public String getName() {
        return BaseFragment.class.getName();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public boolean onKeyBack() {
        return false;
    }


    @Override
    public FragmentManager getFragmentManagerWithinHost() {
        return getFragmentManager();
    }

    @Override
    public Drawable getDrawableWithinHost(@DrawableRes int id) {
        return ViewToolsUtil.getDrawable(getActivity(), id);
    }

    @Override
    public String getStringWithinHost(@StringRes int resId) {
        return getString(resId);
    }

    @Override
    public Resources getResourcesWithinHost() {
        return getResources();
    }

    @Override
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void startActivityForResultWithinHost(Intent intent, int requestCode, @Nullable Bundle options) {
        startActivityForResult(intent, requestCode, options);
    }

    @Override
    public void startActivityForResultWithinHost(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void startActivityWithinHost(Intent intent) {
        startActivity(intent);
    }

    @Override
    public BaseActivity getActivityWithinHost() {
        return (BaseActivity) getActivity();
    }

    @Override
    public Context getContextWithinHost() {
        return getActivity();
    }

    public void dismissSelf() {
        FragmentController.removeFragment(getFragmentManager(), this);
    }

    /**
     * 判断Fragment是否存活
     *
     * @return boolean
     */
    public boolean isFragmentAlive() {
        return isAdded() && !isDetached() && !isRemoving() && getFragmentManagerWithinHost() != null && !getFragmentManagerWithinHost().isDestroyed();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!compositeDisposable.isDisposed()) {
            compositeDisposable.dispose();
        }
    }
}
